﻿using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace Sidvall.UI.Uwp.Converters
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        #region Public Members

        #region Inverted

        private bool _Inverted = false;
        public bool Inverted
        {
            get
            {
                return _Inverted;
            }
            set
            {
                _Inverted = value;
            }
        }

        #endregion
        #region Not

        private bool _Not = false;
        public bool Not
        {
            get
            {
                return _Not;
            }
            set
            {
                _Not = value;
            }
        }

        #endregion

        #region Convert

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return Inverted ? VisibilityToBool(value) : BoolToVisibility(value);
        }

        #endregion
        #region ConvertBack

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return Inverted ? BoolToVisibility(value) : VisibilityToBool(value);
        }

        #endregion

        #endregion
        #region Private Members

        #region VisibilityToBool

        private object VisibilityToBool(object value)
        {
            if (!(value is Visibility))
                return DependencyProperty.UnsetValue;
            switch ((Visibility)value)
            {
                case Visibility.Collapsed:
                    return this.Not;
                default:
                    return !this.Not;
            }
        }

        #endregion
        #region BoolToVisibility

        private object BoolToVisibility(object value)
        {
            if (value == null)
            {
                return Visibility.Collapsed;
            }
            if (!(value is bool))
                return DependencyProperty.UnsetValue;
            return ((bool)value ^ Not) ? Visibility.Visible : Visibility.Collapsed;
        }

        #endregion

        #endregion
    }
}
